/*
 * Created by JFormDesigner on Sun Jul 07 21:17:38 HKT 2024
 */

package com.hongyan.dialog;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;

import com.hongyan.bean.Class;
import com.hongyan.bean.Homework;
import com.hongyan.config.MainFrameConfig;
import com.hongyan.dao.ClassDao;
import com.hongyan.dao.HomeworkDao;
import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.demos.datepicker.*;

/**
 * @author hongyan
 */
public class SwingTeacherTestWorkAddDialog extends JDialog {
    public SwingTeacherTestWorkAddDialog(Window owner) {
        super(owner);
        initComponents();
        initData();
    }

    private void initData() {
        jxDatePicker2.setDate(new Date());
        jxDatePicker3.setDate(new Date());
        List<Class> classList = ClassDao.getClassList();
        Object[] array = classList.stream().map(Class::getC_name).toArray();
        comboBox1.setModel(new DefaultComboBoxModel<>(array));
        cancelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if (textField1.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "作业标题不能为空");
                    return;
                }
                if (textArea1.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "作业内容不能为空");
                    return;
                }
                if (jxDatePicker2.getDate().getTime() > jxDatePicker3.getDate().getTime()) {
                    JOptionPane.showMessageDialog(null, "截止时间不能小于开始时间");
                    return;
                }

                Homework homework = new Homework();
                homework.setH_id(MainFrameConfig.getSnowflakeUtils().getId());
                homework.setH_title(textField1.getText());
                homework.setContent(textArea1.getText());
                homework.setStart_time(new Timestamp(jxDatePicker2.getDate().getTime()));
                homework.setEnd_time(new Timestamp(jxDatePicker3.getDate().getTime()));
                homework.setC_id(classList.get(comboBox1.getSelectedIndex()).getC_id());
                homework.setT_grade(null);
                homework.setS_submission(null);
                int i = HomeworkDao.addHomework(homework);
                if (i > 0) {
                    JOptionPane.showMessageDialog(null, "添加成功");
                } else {
                    JOptionPane.showMessageDialog(null, "添加失败");
                }
                dispose();
            }
        });
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        scrollPane1 = new JScrollPane();
        textArea1 = new JTextArea();
        label3 = new JLabel();
        jxDatePicker2 = new JXDatePicker();
        label4 = new JLabel();
        jxDatePicker3 = new JXDatePicker();
        label5 = new JLabel();
        comboBox1 = new JComboBox();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u6d4b\u8bd5\u4f5c\u4e1a");
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- label1 ----
                label1.setText("\u6807\u9898\uff1a");
                contentPanel.add(label1);
                label1.setBounds(new Rectangle(new Point(50, 0), label1.getPreferredSize()));
                contentPanel.add(textField1);
                textField1.setBounds(100, 0, 185, textField1.getPreferredSize().height);

                //---- label2 ----
                label2.setText("\u5185\u5bb9\uff1a");
                contentPanel.add(label2);
                label2.setBounds(50, 40, 39, 17);

                //======== scrollPane1 ========
                {
                    scrollPane1.setViewportView(textArea1);
                }
                contentPanel.add(scrollPane1);
                scrollPane1.setBounds(100, 40, 185, scrollPane1.getPreferredSize().height);

                //---- label3 ----
                label3.setText("\u5f00\u59cb\u65f6\u95f4\uff1a");
                contentPanel.add(label3);
                label3.setBounds(25, 80, 70, 17);
                contentPanel.add(jxDatePicker2);
                jxDatePicker2.setBounds(100, 75, 185, jxDatePicker2.getPreferredSize().height);

                //---- label4 ----
                label4.setText("\u7ed3\u675f\u65f6\u95f4\uff1a");
                contentPanel.add(label4);
                label4.setBounds(25, 120, 70, 17);
                contentPanel.add(jxDatePicker3);
                jxDatePicker3.setBounds(100, 115, 185, jxDatePicker3.getPreferredSize().height);

                //---- label5 ----
                label5.setText("\u6d4b\u8bd5\u73ed\u7ea7\uff1a");
                contentPanel.add(label5);
                label5.setBounds(25, 155, 70, 17);
                contentPanel.add(comboBox1);
                comboBox1.setBounds(100, 150, 185, comboBox1.getPreferredSize().height);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText("\u6dfb\u52a0");
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText("\u53d6\u6d88");
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JScrollPane scrollPane1;
    private JTextArea textArea1;
    private JLabel label3;
    private JXDatePicker jxDatePicker2;
    private JLabel label4;
    private JXDatePicker jxDatePicker3;
    private JLabel label5;
    private JComboBox comboBox1;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
